package com.yxx.data.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxx.data.domain.InspectionIndex;
import com.yxx.data.service.InspectionIndexService;
import com.yxx.data.vo.InspectionIndexVo;
import com.yxx.sys.common.AppSecureUtils;
import com.yxx.sys.common.DataGridView;
import com.yxx.sys.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxx
 * @since 2022-07-02
 */
@RestController
@RequestMapping("/inspection-index")
@Api("实验室指标控制器")
public class InspectionIndexController {
  @Autowired
  InspectionIndexService inspectionIndexService;

  @GetMapping("/loadAllInformation")
  @ApiOperation("查询实验室指标")
  public DataGridView loadAllInformation(InspectionIndexVo inspectionIndexVo) {
    IPage<InspectionIndex> page = new Page<InspectionIndex>(inspectionIndexVo.getPage(), inspectionIndexVo.getLimit());
    QueryWrapper<InspectionIndex> queryWrapper = new QueryWrapper<InspectionIndex>();
    queryWrapper.eq(StrUtil.isNotBlank(inspectionIndexVo.getHospitalId()), "hospital_id", inspectionIndexVo.getHospitalId());
    queryWrapper.orderByDesc("id");
    inspectionIndexService.page(page, queryWrapper);
    return new DataGridView(page.getTotal(), page.getRecords());
  }

  @PostMapping("/addInformation")
  @ApiOperation("添加实验室指标")
  public Result addInformation(InspectionIndexVo inspectionIndexVo) {
    inspectionIndexVo.setName(AppSecureUtils.getEncryptData(inspectionIndexVo.getName()));

    inspectionIndexVo.setCreatedTime(LocalDate.now());
    inspectionIndexVo.setChangeTime(LocalDate.now());
    boolean succ = inspectionIndexService.save(inspectionIndexVo);
    if(!succ) return Result.ADD_REEOR;
    return Result.ADD_SUCCESS;
  }

  @PostMapping("/updateInformation")
  @ApiOperation("更新实验室指标")
  public Result updateInformation(InspectionIndexVo inspectionIndexVo) {
    try {
      boolean b = inspectionIndexService.updateById(inspectionIndexVo);
      if(!b) return Result.UPDATE_REEOR;
      return Result.UPDATE_SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return Result.UPDATE_REEOR;
    }
  }

  @PostMapping("/deleteInformation")
  @ApiOperation("删除实验室指标")
  public Result deleteInformation(Integer id) {
    try {
      inspectionIndexService.removeById(id);
      return Result.DELETE_SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return Result.DELETE_REEOR;
    }
  }

}
